package dao.Supper.impl;

import dao.BaseDao;
import dao.Supper.SuperSmallDao;
import pojo.BigClass;
import pojo.Smallclass;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SuperSmallDaImpl extends BaseDao implements SuperSmallDao {
    /**
     * 抽出查询公共方法
     * @param sql
     * @param params
     * @return
     */
    public  List<Smallclass> SelSsml(String sql,Object[] params){
        List<Smallclass> list = new ArrayList<>();
        ResultSet rs = this.executeSelect(sql,params);
        try {
            StringBuffer sqls = new StringBuffer("SELECT * FROM smallclass,bigclass WHERE smallBigId=bigclass.id");
            while (rs.next()){
                Smallclass smallclass = new Smallclass();
                smallclass.setSmallId(rs.getLong("smallclass.id"));
                smallclass.setSmallName(rs.getString("smallName"));
                smallclass.setSmallText(rs.getString("smallText"));
                smallclass.setSmallBigId(rs.getLong("smallBigId"));
                BigClass bigClass = new BigClass();
                bigClass.setBigId(rs.getLong("bigclass.id"));
                bigClass.setBigName(rs.getString("bigName"));
                bigClass.setBigText(rs.getString("bigText"));
                smallclass.setBigClass(bigClass);
                list.add(smallclass);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeAll();
        }
        return list;
    }
    /**
     * 计算总页数
     * @param smallName
     * @param smallBigId
     * @return
     */
    public int getTotalCount(String smallName, Long smallBigId) {
        int res = 0;
      try{
          StringBuffer sql = new StringBuffer("SELECT count(*) FROM smallclass,bigclass WHERE smallBigId=bigclass.id");
          if(smallName!=null && smallName!=""){
              sql.append(" and smallName like " + "\'%" + smallName + "%\'");
          }
          if(smallBigId>0){
              sql.append(" and `smallBigId` ="+smallBigId);
          }
          ResultSet rs = this.executeSelect(sql.toString(), null);

          if (rs != null) {
              while (rs.next()) {
                  res = rs.getInt(1);
              }
          }
      }catch (Exception e){
          e.printStackTrace();
      }finally {
          closeAll();
      }
       return res;
    }

    /**
     * 分页查询数据
     * @param currentPage
     * @param pageSize
     * @param smallName
     * @param smallBigId
     * @return
     */
    public List<Smallclass> selSsmlr(int currentPage, int pageSize, String smallName, Long smallBigId) {
        StringBuffer sql = new StringBuffer("SELECT * FROM smallclass,bigclass WHERE smallBigId=bigclass.id");
        if(smallName!=null && smallName!=""){
            sql.append(" and smallName like " + "\'%" + smallName + "%\'");
        }
        if(smallBigId>0){
            sql.append(" and `smallBigId` ="+smallBigId);
        }
        sql.append(" limit ?,?");
        Object[] params = {(currentPage-1)*pageSize,pageSize};
        return SelSsml(sql.toString(),params);
    }

    /**
     * 根据id进行查询
     * @param id
     * @return
     */
    public List<Smallclass> selSsmlById(Long id) {
        String sql = "SELECT * FROM smallclass,bigclass WHERE smallBigId=bigclass.id AND smallclass.id =?";
        Object[] params = {id};
        return SelSsml(sql,params);
    }

    /**
     * 更新
     * @param smallclass
     * @return
     */
    public int updateSsml(Smallclass smallclass) {
        int res=0;
        String sql ="UPDATE `smallclass` SET`smallName`=?,`smallBigId`=?,`smallText`=?  WHERE `id`=?";
        Object[] params = {smallclass.getSmallName(),smallclass.getSmallBigId(),smallclass.getSmallText(),smallclass.getSmallId()};
        res=this.executeUpdateRows(sql,params);
        closeAll();
        return res;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public int delSsml(Long id) {
        int res=0;
        String sql = "delete from smallclass where id=? and id not in(select goodsSmalId from goods)";
        Object[] params ={id};
        res=this.executeUpdateRows(sql,params);
        closeAll();
        return res;
    }

    /**
     * 修改
     * @param smallclass
     * @return
     */
    public int addSsml(Smallclass smallclass) {
        int res =0;
        String sql = "INSERT INTO `smallclass`(`smallName`,`smallBigId`,`smallText`) VALUES(?,?,?)";
        Object[] params ={smallclass.getSmallName(),smallclass.getSmallBigId(),smallclass.getSmallText()};
        res=this.executeUpdateRows(sql,params);
        closeAll();
        return res;
    }

    /**
     *
     * 验证小分类昵称
     * @param name
     * @return
     */
    public int checkSmallName(String name) {
        int count = 0;
        String sql = " SELECT COUNT(`smallName`) FROM `smallclass` WHERE `smallName` =?";
        Object[] params ={name};
        ResultSet rs = this.executeSelect(sql, params);
        try {
            if (rs != null) {
                while (rs.next()) {
                    count = rs.getInt(1);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeAll();
        }
        return count;
    }
}
